using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BaseAmd.BaseData;
using BaseAmd.Ret;
using HslCommunication.Core;
using Catastrophe.AutoSulution.EDC;
using Catastrophe.AutoSulution.TaskM;

namespace SiemensAmd.BasicData.S7Ex
{
	public class SieCncS7Ex
	{
		private Socket _socket;

		private readonly SimpleHybirdLock _readWriteLock;

		public bool IsConnected => this._socket?.Connected ?? false;

		public int ReadWriteTimeout { get; set; } = 3000;


		public int VersionSeqNo { get; set; }

		public int MachineMode { get; set; }

		public int JogFeedSeqNo { get; set; }

		public int WorkAxisSeqNo { get; set; }

		public int AlarmSeqNo { get; set; }

		public int ProgSeqNo { get; set; }

		public SieCncS7Ex()
		{
			this._readWriteLock = new SimpleHybirdLock();
		}

		public OperateAmdRet Connect(string ip, int port, int timeout = 3000)
		{
			if (!IPAddress.TryParse(ip, out var ipFormat))
			{
				return new OperateAmdRet
				{
					Success = false,
					Message = "IP地址格式错误"
				};
			}
			Tuple<Task, Exception> tuple = TaskManager.StartTo(delegate
			{
				this._socket = new Socket(ipFormat.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
				this._socket.Connect(new IPEndPoint(ipFormat, port));
				this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, this.ReadWriteTimeout);
				this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, this.ReadWriteTimeout);
				this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Debug, 1);
			}, timeout);
			OperateAmdRet operateAmdRet = new OperateAmdRet
			{
				Success = (tuple.Item2 == null && tuple.Item1.IsCompleted && (this._socket?.Connected ?? false)),
				Message = tuple.Item2?.ToString()
			};
			if (!operateAmdRet.Success)
			{
				return operateAmdRet;
			}
			return this.ConnectInit();
		}

		public void Disconnect()
		{
			try
			{
				if (this._socket != null)
				{
					if (this._socket.Connected)
					{
						this._socket.Shutdown(SocketShutdown.Both);
						this._socket.Close();
						this._socket.Dispose();
					}
					else
					{
						this._socket.Close();
						this._socket.Dispose();
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		private OperateAmdRet ConnectInit()
		{
			OperateAmdRet operateAmdRet = new OperateAmdRet();
			GOperateAmdRet<byte[]> gOperateAmdRet = this.WriteSync(SieCncS7Cmd.InitCmd1, 3000, 0);
			if (!gOperateAmdRet.Success)
			{
				operateAmdRet.Message = "init1 failed! " + gOperateAmdRet.Message;
				return operateAmdRet;
			}
			Thread.Sleep(100);
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.InitCmd2, 3000, 0);
			if (!gOperateAmdRet2.Success)
			{
				operateAmdRet.Message = "init2 failed! " + gOperateAmdRet2.Message;
				return operateAmdRet;
			}
			Thread.Sleep(100);
			GOperateAmdRet<byte[]> gOperateAmdRet3 = this.WriteSync(SieCncS7Cmd.InitCmd3, 3000, 0);
			if (!gOperateAmdRet3.Success)
			{
				operateAmdRet.Message = "init3 failed! " + gOperateAmdRet3.Message;
				return operateAmdRet;
			}
			Thread.Sleep(100);
			operateAmdRet.Success = true;
			return operateAmdRet;
		}

		private GOperateAmdRet<byte[]> WriteSync(byte[] buff, int timeout = 3000, int sendEndTimes = 2, int sendEndWait = 500)
		{
			GOperateAmdRet<byte[]> gOperateAmdRet = new GOperateAmdRet<byte[]>();
			Socket socket = this._socket;
			if (socket == null || !socket.Connected)
			{
				gOperateAmdRet.Message = "please connect to server first!";
				return gOperateAmdRet;
			}
			this._readWriteLock.Enter();
			try
			{
				Tuple<Task<GOperateAmdRet<byte[]>>, Exception> tuple = TaskManager.StartTo(delegate
				{
					GOperateAmdRet<byte[]> gOperateAmdRet2 = new GOperateAmdRet<byte[]>();
					try
					{
						List<byte> list = new List<byte>();
						this._socket.Send(buff, 0, buff.Length, SocketFlags.None);
						Thread.Sleep(30);
						int num = 0;
						while (this._socket.Available > 0)
						{
							num++;
							byte[] array = new byte[this._socket.Available];
							int num2 = this._socket.Receive(array, 0, array.Length, SocketFlags.None);
							list.AddRange(array);
							Thread.Sleep(30);
						}
						if (list.Count > 0)
						{
							gOperateAmdRet2.Success = true;
							gOperateAmdRet2.Content = list.ToArray();
							gOperateAmdRet2.Message = "";
						}
						else
						{
							gOperateAmdRet2.Message = "read count error!";
						}
						return gOperateAmdRet2;
					}
					catch (Exception ex)
					{
						gOperateAmdRet2.Message = ex.ToString();
						return gOperateAmdRet2;
					}
				}, timeout);
				if (!tuple.Item1.IsCompleted)
				{
					gOperateAmdRet.Message = "send timeout!";
					return gOperateAmdRet;
				}
				if (tuple.Item1.Result.Success && sendEndTimes > 0)
				{
					Thread.Sleep(sendEndWait);
					OperateAmdRet operateAmdRet = this.RequestEnd(sendEndTimes, isLock: false);
					if (!operateAmdRet.Success)
					{
						gOperateAmdRet.Message = "send end failed " + operateAmdRet.Message;
						return gOperateAmdRet;
					}
				}
				return tuple.Item1.Result;
			}
			finally
			{
				this._readWriteLock.Leave();
			}
		}

		private OperateAmdRet WriteSyncWithoutReturn(byte[] buff, int timeout = 3000, bool isLock = true)
		{
			OperateAmdRet operateAmdRet = new OperateAmdRet();
			Socket socket = this._socket;
			if (socket == null || !socket.Connected)
			{
				operateAmdRet.Message = "please connect to server first!";
				return operateAmdRet;
			}
			if (isLock)
			{
				this._readWriteLock.Enter();
			}
			try
			{
				Tuple<Task<OperateAmdRet>, Exception> tuple = TaskManager.StartTo(delegate
				{
					OperateAmdRet operateAmdRet2 = new OperateAmdRet();
					try
					{
						int num = this._socket.Send(buff, 0, buff.Length, SocketFlags.None);
						operateAmdRet2.Success = num > 0;
						return operateAmdRet2;
					}
					catch (Exception ex)
					{
						operateAmdRet2.Message = ex.ToString();
						return operateAmdRet2;
					}
				}, timeout);
				if (!tuple.Item1.IsCompleted)
				{
					operateAmdRet.Message = "send timeout!";
					return operateAmdRet;
				}
				return tuple.Item1.Result;
			}
			finally
			{
				if (isLock)
				{
					this._readWriteLock.Leave();
				}
			}
		}

		public GOperateAmdRet<string> ReadVersion()
		{
			GOperateAmdRet<string> gOperateAmdRet = new GOperateAmdRet<string>();
			StringBuilder stringBuilder = new StringBuilder();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadVersionCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			byte[] content = gOperateAmdRet2.Content;
			int num = content.Length;
			List<byte> list = new List<byte>();
			if (content[12] == 15)
			{
				this.VersionSeqNo = content[24];
			}
			int num2 = 0;
			int num3 = 34;
			if (content[num3 + 1] == byte.MaxValue && content[num3 + 2] == 9)
			{
				num2 = content[num3 + 4];
				for (int i = 0; i < num && content[i + num3 + 5] != 0; i++)
				{
					list.Add(content[i + num3 + 5]);
				}
				string text = EnDeCode.ConvertByteBufToT<string>(list.ToArray());
				stringBuilder.Append(text + Environment.NewLine);
			}
			num3 += 4;
			num3 += num2;
			list.Clear();
			if (content[num3 + 1] == byte.MaxValue && content[num3 + 2] == 9)
			{
				num2 = content[num3 + 4];
				for (int j = 0; j < num && content[j + num3 + 5] != 0; j++)
				{
					list.Add(content[j + num3 + 5]);
				}
				string text2 = EnDeCode.ConvertByteBufToT<string>(list.ToArray());
				stringBuilder.Append(text2 + Environment.NewLine);
			}
			num3 += 4;
			num3 += num2;
			list.Clear();
			if (content[num3 + 1] == byte.MaxValue && content[num3 + 2] == 9)
			{
				num2 = content[num3 + 4];
				for (int k = 0; k < num && content[k + num3 + 5] != 0; k++)
				{
					list.Add(content[k + num3 + 5]);
				}
				string text3 = EnDeCode.ConvertByteBufToT<string>(list.ToArray());
			}
			num3 += 4;
			num3 += num2;
			list.Clear();
			if (content[num3 + 1] == byte.MaxValue && content[num3 + 2] == 9)
			{
				num2 = content[num3 + 4];
				for (int l = 0; l < num && content[l + num3 + 5] != 0; l++)
				{
					list.Add(content[l + num3 + 5]);
				}
				string text4 = EnDeCode.ConvertByteBufToT<string>(list.ToArray());
			}
			num3 += 4;
			num3 += num2;
			list.Clear();
			if (content[num3 + 1] == byte.MaxValue && content[num3 + 2] == 9)
			{
				num2 = content[num3 + 4];
				for (int m = 0; m < num; m++)
				{
					if (content[m + num3 + 5] == 84 || content[m + num3 + 5] == 116)
					{
						this.MachineMode = 0;
					}
					else if (content[m + num3 + 5] == 77 || content[m + num3 + 5] == 109)
					{
						this.MachineMode = 1;
					}
					if (content[m + num3 + 5] == 0)
					{
						break;
					}
					list.Add(content[m + num3 + 5]);
				}
				string value = EnDeCode.ConvertByteBufToT<string>(list.ToArray());
				stringBuilder.Append(value);
			}
			gOperateAmdRet.Content = stringBuilder.ToString();
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<int> ReadRunTime()
		{
			GOperateAmdRet<int> gOperateAmdRet = new GOperateAmdRet<int>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadRunTimeCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			GOperateAmdRet<double> @double = this.GetDouble(gOperateAmdRet2.Content);
			gOperateAmdRet.Success = @double.Success;
			gOperateAmdRet.Content = (int)@double.Content;
			gOperateAmdRet.Message = @double.Message;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<int> ReadProgActTime()
		{
			GOperateAmdRet<int> gOperateAmdRet = new GOperateAmdRet<int>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadRunTimeCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			GOperateAmdRet<double> @double = this.GetDouble(gOperateAmdRet2.Content);
			gOperateAmdRet.Success = @double.Success;
			gOperateAmdRet.Content = (int)@double.Content;
			gOperateAmdRet.Message = @double.Message;
			return gOperateAmdRet;
		}

		private GOperateAmdRet<double> GetDouble(byte[] ts, int i = 21)
		{
			GOperateAmdRet<double> gOperateAmdRet = new GOperateAmdRet<double>();
			byte[] array = new byte[8];
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler;
			if (i >= ts.Length)
			{
				defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(14, 1);
				defaultInterpolatedStringHandler.AppendLiteral("rcv len error ");
				defaultInterpolatedStringHandler.AppendFormatted(ts.Length);
				gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return gOperateAmdRet;
			}
			if (ts[i] == byte.MaxValue && ts[i + 1] == 9)
			{
				array[0] = ts[i + 4];
				array[1] = ts[i + 5];
				array[2] = ts[i + 6];
				array[3] = ts[i + 7];
				array[4] = ts[i + 8];
				array[5] = ts[i + 9];
				array[6] = ts[i + 10];
				array[7] = ts[i + 11];
				double num = EnDeCode.ConvertByteBufToT<double>(array, 0, EndianModeEnum.Little);
				gOperateAmdRet.Success = true;
				gOperateAmdRet.Content = (int)num;
				return gOperateAmdRet;
			}
			defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(13, 2);
			defaultInterpolatedStringHandler.AppendLiteral("check error ");
			defaultInterpolatedStringHandler.AppendFormatted(ts[i]);
			defaultInterpolatedStringHandler.AppendLiteral(" ");
			defaultInterpolatedStringHandler.AppendFormatted(ts[i + 1]);
			gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
			return gOperateAmdRet;
		}

		public GOperateAmdRet<int> ReadFeedTime()
		{
			GOperateAmdRet<int> gOperateAmdRet = new GOperateAmdRet<int>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadFeedTime, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			GOperateAmdRet<double> @double = this.GetDouble(gOperateAmdRet2.Content);
			gOperateAmdRet.Success = @double.Success;
			gOperateAmdRet.Content = (int)@double.Content;
			gOperateAmdRet.Message = @double.Message;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<int> ReadCoolStartTime()
		{
			GOperateAmdRet<int> gOperateAmdRet = new GOperateAmdRet<int>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadCoolStartTimeCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			GOperateAmdRet<double> @double = this.GetDouble(gOperateAmdRet2.Content);
			gOperateAmdRet.Success = @double.Success;
			gOperateAmdRet.Content = (int)@double.Content;
			gOperateAmdRet.Message = @double.Message;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<int> ReadSetupTime()
		{
			GOperateAmdRet<int> gOperateAmdRet = new GOperateAmdRet<int>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadSetupTimeCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			GOperateAmdRet<double> @double = this.GetDouble(gOperateAmdRet2.Content);
			gOperateAmdRet.Success = @double.Success;
			gOperateAmdRet.Content = (int)@double.Content;
			gOperateAmdRet.Message = @double.Message;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<SiePartTimeEntity> ReadPieceTime()
		{
			GOperateAmdRet<SiePartTimeEntity> gOperateAmdRet = new GOperateAmdRet<SiePartTimeEntity>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadPieceTimeCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new SiePartTimeEntity();
			byte[] content = gOperateAmdRet2.Content;
			int num = 35;
			for (int i = 0; i < 4; i++)
			{
				if (i == 0)
				{
					double content2 = this.GetDouble(content, num).Content;
					gOperateAmdRet.Content.RunTime = (int)content2;
				}
				if (i == 1)
				{
					double content3 = this.GetDouble(content, num).Content;
					gOperateAmdRet.Content.ProgRemainTime = (int)content3;
				}
				if (i == 2)
				{
					int partCountAct = (int)this.GetDouble(content, num).Content;
					gOperateAmdRet.Content.PartCountAct = partCountAct;
				}
				if (i == 3)
				{
					int partCountSet = (int)this.GetDouble(content, num).Content;
					gOperateAmdRet.Content.PartCountSet = partCountSet;
				}
				num += 12;
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<SieOvrEntity> ReadFeed(int opMode)
		{
			if (opMode == 0)
			{
				return this.ReadJogFeed();
			}
			return this.ReadAutoFeed();
		}

		public GOperateAmdRet<SieOvrEntity> ReadJogFeed()
		{
			GOperateAmdRet<SieOvrEntity> gOperateAmdRet = new GOperateAmdRet<SieOvrEntity>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadAxisValueCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new SieOvrEntity();
			byte[] content = gOperateAmdRet2.Content;
			if (content.Length < 39)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(14, 1);
				defaultInterpolatedStringHandler.AppendLiteral("rcv len error ");
				defaultInterpolatedStringHandler.AppendFormatted(content.Length);
				gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return gOperateAmdRet;
			}
			if (content[12] == 7)
			{
				this.JogFeedSeqNo = content[24];
			}
			if (content[38] == 8)
			{
				double feedSpeedAct = EnDeCode.ConvertByteBufToT<double>(content, 39, EndianModeEnum.Little);
				gOperateAmdRet.Content.FeedSpeedAct = feedSpeedAct;
			}
			if (content[50] == 8)
			{
				double feedSpeedSet = EnDeCode.ConvertByteBufToT<double>(content, 51, EndianModeEnum.Little);
				gOperateAmdRet.Content.FeedSpeedSet = feedSpeedSet;
			}
			if (content[62] == 8)
			{
				double feedRate = EnDeCode.ConvertByteBufToT<double>(content, 63, EndianModeEnum.Little);
				gOperateAmdRet.Content.FeedRate = feedRate;
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<SieOvrEntity> ReadAutoFeed()
		{
			GOperateAmdRet<SieOvrEntity> gOperateAmdRet = new GOperateAmdRet<SieOvrEntity>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadPieceTimeCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new SieOvrEntity();
			byte[] content = gOperateAmdRet2.Content;
			byte[] array = new byte[8];
			if (content.Length < 39)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(14, 1);
				defaultInterpolatedStringHandler.AppendLiteral("rcv len error ");
				defaultInterpolatedStringHandler.AppendFormatted(content.Length);
				gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return gOperateAmdRet;
			}
			if (content[38] == 8)
			{
				Array.Clear(array, 0, array.Length);
				for (int i = 0; i < 8; i++)
				{
					array[i] = content[39 + i];
				}
				Array.Reverse(array);
				double feedSpeedAct = EnDeCode.ConvertByteBufToT<double>(array);
				gOperateAmdRet.Content.FeedSpeedAct = feedSpeedAct;
			}
			if (content[50] == 8)
			{
				Array.Clear(array, 0, array.Length);
				for (int j = 0; j < 8; j++)
				{
					array[j] = content[51 + j];
				}
				Array.Reverse(array);
				double feedSpeedSet = EnDeCode.ConvertByteBufToT<double>(array);
				gOperateAmdRet.Content.FeedSpeedSet = feedSpeedSet;
			}
			if (content[62] == 8)
			{
				Array.Clear(array, 0, array.Length);
				for (int k = 0; k < 8; k++)
				{
					array[k] = content[63 + k];
				}
				Array.Reverse(array);
				double feedRate = EnDeCode.ConvertByteBufToT<double>(array);
				gOperateAmdRet.Content.FeedRate = feedRate;
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<SieOvrEntity> ReadSpindleOvr()
		{
			GOperateAmdRet<SieOvrEntity> gOperateAmdRet = new GOperateAmdRet<SieOvrEntity>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadSpindleOvrCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new SieOvrEntity();
			byte[] content = gOperateAmdRet2.Content;
			byte[] array = new byte[8];
			if (content.Length < 39)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(14, 1);
				defaultInterpolatedStringHandler.AppendLiteral("rcv len error ");
				defaultInterpolatedStringHandler.AppendFormatted(content.Length);
				gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return gOperateAmdRet;
			}
			if (content[38] == 8)
			{
				Array.Clear(array, 0, array.Length);
				for (int i = 0; i < 8; i++)
				{
					array[i] = content[39 + i];
				}
				Array.Reverse(array);
				double spindleSpeedAct = EnDeCode.ConvertByteBufToT<double>(array);
				gOperateAmdRet.Content.SpindleSpeedAct = spindleSpeedAct;
			}
			if (content[50] == 8)
			{
				Array.Clear(array, 0, array.Length);
				for (int j = 0; j < 8; j++)
				{
					array[j] = content[51 + j];
				}
				Array.Reverse(array);
				double spindleSpeedSet = EnDeCode.ConvertByteBufToT<double>(array);
				gOperateAmdRet.Content.SpindleSpeedSet = spindleSpeedSet;
			}
			if (content[62] == 8)
			{
				Array.Clear(array, 0, array.Length);
				for (int k = 0; k < 8; k++)
				{
					array[k] = content[63 + k];
				}
				Array.Reverse(array);
				double spindleRate = EnDeCode.ConvertByteBufToT<double>(array);
				gOperateAmdRet.Content.SpindleRate = spindleRate;
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<SieOprModeEntity> ReadOprMode()
		{
			GOperateAmdRet<SieOprModeEntity> gOperateAmdRet = new GOperateAmdRet<SieOprModeEntity>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadOprateModeCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new SieOprModeEntity();
			byte[] content = gOperateAmdRet2.Content;
			if (content.Length < 39)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(14, 1);
				defaultInterpolatedStringHandler.AppendLiteral("rcv len error ");
				defaultInterpolatedStringHandler.AppendFormatted(content.Length);
				gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return gOperateAmdRet;
			}
			if (content[38] == 2)
			{
				if (content[45] == 0)
				{
					gOperateAmdRet.Content.OprMode = content[39];
				}
				else if (content[45] == 3)
				{
					gOperateAmdRet.Content.OprMode = -1;
				}
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<string> ReadCycState()
		{
			GOperateAmdRet<string> gOperateAmdRet = new GOperateAmdRet<string>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadCycState, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			byte[] content = gOperateAmdRet2.Content;
			if (content.Length < 39)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(14, 1);
				defaultInterpolatedStringHandler.AppendLiteral("rcv len error ");
				defaultInterpolatedStringHandler.AppendFormatted(content.Length);
				gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return gOperateAmdRet;
			}
			if (content[38] == 2)
			{
				if (content[39] == 0 && content[45] == 5)
				{
					gOperateAmdRet.Content = "RES SPENDLE_STOP";
				}
				if (content[39] == 2 && content[45] == 2)
				{
					gOperateAmdRet.Content = "CYCSTOP SPENDLE_CW";
				}
				if (content[39] == 1 && content[45] == 3)
				{
					gOperateAmdRet.Content = "CYCSTART SPENDLE_CW";
				}
				if (content[39] == 1 && content[45] == 5)
				{
					gOperateAmdRet.Content = "SPENDLE_CW/CCW";
				}
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<List<BaseAxisInfoEntity>> ReadAxisMach()
		{
			GOperateAmdRet<List<BaseAxisInfoEntity>> gOperateAmdRet = new GOperateAmdRet<List<BaseAxisInfoEntity>>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadAxisValueCmd, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new List<BaseAxisInfoEntity>();
			BaseAxisInfoEntity baseAxisInfoEntity = new BaseAxisInfoEntity();
			BaseAxisInfoEntity baseAxisInfoEntity2 = new BaseAxisInfoEntity();
			BaseAxisInfoEntity baseAxisInfoEntity3 = new BaseAxisInfoEntity();
			byte[] content = gOperateAmdRet2.Content;
			int num = 35;
			for (int i = 0; i < 12; i++)
			{
				if (num >= content.Length)
				{
					break;
				}
				if (content[num] != byte.MaxValue || content[num + 1] != 9)
				{
					continue;
				}
				double content2 = this.GetDouble(content, num).Content;
				switch (i)
				{
				case 0:
					baseAxisInfoEntity.Name = "MX1";
					baseAxisInfoEntity.MachPosition = content2;
					break;
				case 1:
					if (this.MachineMode == 0)
					{
						baseAxisInfoEntity3.Name = "MZ1";
						baseAxisInfoEntity3.MachPosition = content2;
					}
					else
					{
						baseAxisInfoEntity2.Name = "MY1";
						baseAxisInfoEntity2.MachPosition = content2;
					}
					break;
				case 2:
					baseAxisInfoEntity3.Name = "MZ1";
					baseAxisInfoEntity3.MachPosition = content2;
					break;
				case 4:
					baseAxisInfoEntity.Remain = content2;
					break;
				case 5:
					if (this.MachineMode == 0)
					{
						baseAxisInfoEntity3.Remain = content2;
					}
					else
					{
						baseAxisInfoEntity2.Remain = content2;
					}
					break;
				case 6:
					if (this.MachineMode == 1)
					{
						baseAxisInfoEntity3.Remain = content2;
					}
					break;
				}
				num += 12;
			}
			if (baseAxisInfoEntity.Name != null)
			{
				gOperateAmdRet.Content.Add(baseAxisInfoEntity);
			}
			if (baseAxisInfoEntity2.Name != null)
			{
				gOperateAmdRet.Content.Add(baseAxisInfoEntity2);
			}
			if (baseAxisInfoEntity3.Name != null)
			{
				gOperateAmdRet.Content.Add(baseAxisInfoEntity3);
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<List<BaseAxisInfoEntity>> ReadAxisAbs()
		{
			GOperateAmdRet<List<BaseAxisInfoEntity>> gOperateAmdRet = new GOperateAmdRet<List<BaseAxisInfoEntity>>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadWorkAxis, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new List<BaseAxisInfoEntity>();
			BaseAxisInfoEntity baseAxisInfoEntity = new BaseAxisInfoEntity();
			BaseAxisInfoEntity baseAxisInfoEntity2 = new BaseAxisInfoEntity();
			BaseAxisInfoEntity baseAxisInfoEntity3 = new BaseAxisInfoEntity();
			byte[] content = gOperateAmdRet2.Content;
			int num = 35;
			for (int i = 0; i < 4; i++)
			{
				double content2 = this.GetDouble(content, num).Content;
				switch (i)
				{
				case 0:
					baseAxisInfoEntity.Name = "X";
					baseAxisInfoEntity.AbsPosition = content2;
					break;
				case 1:
					if (this.MachineMode == 0)
					{
						baseAxisInfoEntity3.Name = "Z";
						baseAxisInfoEntity3.AbsPosition = content2;
					}
					else
					{
						baseAxisInfoEntity2.Name = "Y";
						baseAxisInfoEntity2.AbsPosition = content2;
					}
					break;
				case 2:
					baseAxisInfoEntity3.Name = "Z";
					baseAxisInfoEntity3.AbsPosition = content2;
					break;
				}
				num += 12;
			}
			if (baseAxisInfoEntity.Name != null)
			{
				gOperateAmdRet.Content.Add(baseAxisInfoEntity);
			}
			if (baseAxisInfoEntity2.Name != null)
			{
				gOperateAmdRet.Content.Add(baseAxisInfoEntity2);
			}
			if (baseAxisInfoEntity3.Name != null)
			{
				gOperateAmdRet.Content.Add(baseAxisInfoEntity3);
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<List<int>> ReadAlarm()
		{
			GOperateAmdRet<List<int>> gOperateAmdRet = new GOperateAmdRet<List<int>>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadAlam, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			gOperateAmdRet.Content = new List<int>();
			byte[] content = gOperateAmdRet2.Content;
			string text = EnDeCode.ByteToHexStr(content, content.Length);
			for (int i = 0; i < content.Length - 4; i++)
			{
				if (content[i] == byte.MaxValue && content[i + 1] == 9 && content[i + 2] == 0 && content[i + 3] == 156)
				{
					ushort item = EnDeCode.ConvertByteBufToT<ushort>(content, i + 4, EndianModeEnum.Little);
					if (!gOperateAmdRet.Content.Contains(item))
					{
						gOperateAmdRet.Content.Add(item);
					}
				}
			}
			gOperateAmdRet.Success = true;
			return gOperateAmdRet;
		}

		public GOperateAmdRet<string> ReadProgNow()
		{
			GOperateAmdRet<string> gOperateAmdRet = new GOperateAmdRet<string>();
			if (!this.IsConnected)
			{
				gOperateAmdRet.Message = "please connect server first!";
				return gOperateAmdRet;
			}
			GOperateAmdRet<byte[]> gOperateAmdRet2 = this.WriteSync(SieCncS7Cmd.ReadProgNow, this.ReadWriteTimeout);
			if (!gOperateAmdRet2.Success)
			{
				gOperateAmdRet.Message = gOperateAmdRet2.Message;
				return gOperateAmdRet;
			}
			string text = "";
			string text2 = "";
			byte[] content = gOperateAmdRet2.Content;
			if (content[3] == 199 && content[12] == 18)
			{
				if (content[12] == 18)
				{
					this.ProgSeqNo = content[24];
				}
				if (content[35] == byte.MaxValue && content[36] == 9)
				{
					gOperateAmdRet.Success = true;
					for (int i = 39; content[i] != 0 && i < content[3]; i++)
					{
						text += Encoding.ASCII.GetString(new byte[1] { content[i] });
						if (i > 50)
						{
							text2 += Encoding.ASCII.GetString(new byte[1] { content[i] });
						}
					}
				}
			}
			else if (content[3] == 57 && content[12] == 18)
			{
				gOperateAmdRet.Success = true;
				for (int j = 25; content[j] != 0 && j < content[3]; j++)
				{
					text += Encoding.ASCII.GetString(new byte[1] { content[j] });
					if (j > 50)
					{
						text2 += Encoding.ASCII.GetString(new byte[1] { content[j] });
					}
				}
			}
			else
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(8, 2);
				defaultInterpolatedStringHandler.AppendLiteral("3(");
				defaultInterpolatedStringHandler.AppendFormatted(content[3], "X2");
				defaultInterpolatedStringHandler.AppendLiteral("),12(");
				defaultInterpolatedStringHandler.AppendFormatted(content[12], "x2");
				defaultInterpolatedStringHandler.AppendLiteral(")");
				gOperateAmdRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
			}
			gOperateAmdRet.Content = text;
			return gOperateAmdRet;
		}

		public OperateAmdRet RequestEnd(int times = 2, bool isLock = true)
		{
			if (times <= 0)
			{
				throw new Exception("invalid param times");
			}
			OperateAmdRet operateAmdRet = new OperateAmdRet();
			byte[] buff = new byte[7] { 3, 0, 0, 7, 2, 240, 0 };
			for (int i = 0; i < times; i++)
			{
				OperateAmdRet operateAmdRet2 = this.WriteSyncWithoutReturn(buff, 3000, isLock);
				if (!operateAmdRet2.Success)
				{
					operateAmdRet.Message = operateAmdRet2.Message;
					return operateAmdRet;
				}
			}
			operateAmdRet.Success = true;
			return operateAmdRet;
		}
	}
}
